package com.didispace;

import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 翟永超
 * @create 2017/4/15.
 * @blog http://blog.didispace.com
 */
@RestController
public class DcController {
	
	
    
    @Autowired
    RestTemplate restTemplate;
    
    @Autowired
    HelloService helloService;
    
    @Autowired
    UserService userService;

    @GetMapping("/consumer")
    public String dc() {
    	
//    	RestTemplate restTemplate = new RestTemplate();
//    	HttpHeaders headers = new HttpHeaders();
//    	MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//    	headers.setContentType(type);
//    	headers.add("Accept", MediaType.APPLICATION_JSON.toString());

//    	String params = "{\"dataList\":[{\"aPPLYCD\":\"BJC2101706000387\",\"nAME\":\"刘云\",\"pHONE\":\"13816620418\"}],\"smsName\":\"JC_BCZL\",\"updateUser\":\"8\"}";
//
//    	HttpEntity<String> formEntity = new HttpEntity<String>(params, headers);
//		ResponseEntity<HashMap> responseEntity = restTemplate.postForEntity("http://192.168.121.9:8890/sms/ws/sendsms.do", formEntity, HashMap.class);
//		HashMap<String, String> urlMap = responseEntity.getBody();

    	// 对于RestTemplate的使用，我们的第一个url参数有一些特别。这里请求的host位置并没有使用一个具体的IP地址和端口的形式，而是采用了服务名的方式组成。
    	// 那么这样的请求为什么可以调用成功呢？因为Spring Cloud Ribbon有一个拦截器，它能够在这里进行实际调用的时候，自动的去选取服务实例，并将实际要请求的IP地址和端口替换这里的服务名，从而完成服务接口的调用。
        return restTemplate.getForObject("http://eureka-client/dc", String.class);
    }
    
    // 初次上手断路器
    @GetMapping("/hystrixStart")
    public String hystrixStart() {
    	return helloService.hystrixStart();
    }
    
    // 初次上手断路器
    @GetMapping("/hystrixSecond")
    public String hystrixSecond() {
    	
    	return helloService.hystrixSecond();
    }
    
 // 初次上手断路器
    @GetMapping("/user")
    public User user() {
    	
    	return userService.syn();
    }
    
    // 初次上手断路器
    @GetMapping("/asyUser")
    public User asyUser() throws InterruptedException, ExecutionException {
    	return userService.asyn();
    }
    
 // 初次上手断路器
    @GetMapping("/annAsyUser")
    public User annAsyUser() throws InterruptedException, ExecutionException {
    	return userService.annAsyUser().get();
    }
    
    @GetMapping("/observeUser")
    public User observeUser() throws InterruptedException, ExecutionException {
    	return userService.observe();
    }
    
    @GetMapping("/HystrixObservableObserve")
    public User HystrixObservableObserve() throws InterruptedException, ExecutionException {
    	return userService.HystrixObservableObserve();
    }
    
    
//    @GetMapping("/annObserve")
//    public User annObserve() throws InterruptedException, ExecutionException {
//    	return userService.annObserve();
//    }
    
    
}
